/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.axiom.ui;

import com.axiom.controller.course_details_controller;
import com.axiom.persistence.course_detail_persist;
import com.axiom.utility.NullCheckUtility;
import com.axiom.utility.PDFGenerator;
import com.axiom.vo.course_detail;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Shuhail
 */
public class viewAllCourses extends javax.swing.JFrame {

    /**
     * Creates new form viewAllCourses
     */
    public viewAllCourses() {
        changeTheLookAndFeel();
        initComponents();
        this.loadData(null);
        this.deleteButton.setEnabled(false);
        
    }
    
    
    public void changeTheLookAndFeel() {
        
         try {
            
            UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(StartHere.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(StartHere.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(StartHere.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StartHere.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
    }
    public void loadData(String name) {
        
        DefaultTableModel dft = new DefaultTableModel();
        jTable1.setModel(dft);
        dft.setColumnIdentifiers(new String[]{"Course Name","Fees"});
        
        Iterator it = list.iterator();
        while(it.hasNext()) {
            
            course_detail detail = (course_detail)it.next();
            System.out.println(detail.getName()+"---"+detail.getFees());
            if(NullCheckUtility.isEmptyOrNull(name))
            dft.addRow(new String[]{detail.getName(),Double.toString(detail.getFees())});
            else
            {
                String course = detail.getName();
                course =  course.toLowerCase();
                name = name.toLowerCase();
                if(course.contains(name))
                    dft.addRow(new String[]{detail.getName(),Double.toString(detail.getFees())});
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        conformDialogue = new javax.swing.JDialog();
        jLabel4 = new javax.swing.JLabel();
        yesConfirm = new javax.swing.JButton();
        noConfirm = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel3 = new javax.swing.JPanel();
        courseNameText = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        msg = new javax.swing.JLabel();

        conformDialogue.setTitle("Conform Delete");
        conformDialogue.setMinimumSize(new java.awt.Dimension(200, 125));
        conformDialogue.setResizable(false);

        jLabel4.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel4.setText("Confirm Delete ?");

        yesConfirm.setText("Yes");
        yesConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                yesConfirmActionPerformed(evt);
            }
        });

        noConfirm.setText("No");
        noConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                noConfirmActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout conformDialogueLayout = new javax.swing.GroupLayout(conformDialogue.getContentPane());
        conformDialogue.getContentPane().setLayout(conformDialogueLayout);
        conformDialogueLayout.setHorizontalGroup(
            conformDialogueLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(conformDialogueLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(conformDialogueLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(conformDialogueLayout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(yesConfirm)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(noConfirm)))
                .addContainerGap(250, Short.MAX_VALUE))
        );
        conformDialogueLayout.setVerticalGroup(
            conformDialogueLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(conformDialogueLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(conformDialogueLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(yesConfirm)
                    .addComponent(noConfirm))
                .addContainerGap(226, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("View Course");

        jPanel1.setBackground(new java.awt.Color(153, 153, 255));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Course Name", "Fees"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Double.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jTable1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTable1KeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTable1KeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 140, 300, 260));

        jButton3.setBackground(new java.awt.Color(51, 0, 255));
        jButton3.setForeground(new java.awt.Color(204, 0, 51));
        jButton3.setText("Ref");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 400, -1, -1));

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Add New");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton4.setText("Save as PDF");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        deleteButton.setText("Delete");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                        .addComponent(deleteButton)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4)
                    .addComponent(deleteButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 440, 210, -1));

        jLabel1.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
        jLabel1.setText("Quick Search");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 10, -1, -1));
        jPanel1.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 38, 300, -1));

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        courseNameText.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                courseNameTextInputMethodTextChanged(evt);
            }
        });
        courseNameText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                courseNameTextKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                courseNameTextKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                courseNameTextKeyTyped(evt);
            }
        });

        jLabel2.setText("Course Name :");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(courseNameText, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(74, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(courseNameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        jPanel1.add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 50, 300, 70));
        jPanel1.add(msg, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 410, 230, 20));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 523, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        new newCourse().setVisible(true);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

        // TODO add your handling code here:
        list = course_detail_persist.getInstance().getAllDetails();
        this.loadData(null);
    }//GEN-LAST:event_jButton3ActionPerformed
    
    public String getTheIdForSelection() {
        
        int row = this.jTable1.getSelectedRow();
        int col = this.jTable1.getSelectedColumn();
        Double feeData = null ;
        String data = null;
        if(col==1) {
            feeData = Double.parseDouble(this.jTable1.getModel().getValueAt(row, col).toString());
        }
        else 
        data = this.jTable1.getModel().getValueAt(row, col).toString();
        Iterator it = list.iterator();
        boolean isDelete=true;
        while(it.hasNext()) {
            
            course_detail detail = (course_detail)it.next();
           if(NullCheckUtility.isEmptyOrNull(data)) {
                String data1 = this.jTable1.getModel().getValueAt(row, 0).toString();
               if(feeData==detail.getFees() && data1.equals(detail.getName()))
                   return detail.getCourse_id();
           }
           else {
               
                 feeData = Double.parseDouble(this.jTable1.getModel().getValueAt(row, 1).toString());
                   
                     if(detail.getName().equals(data) || detail.getFees()==feeData)
                         return detail.getCourse_id();
                     
                
                }
            
        }
        return null;
        
    }
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        
         JFileChooser chooser = new JFileChooser();
         //chooser.setDialogType(JFileChooser.SAVE_DIALOG);
         int result = chooser.showSaveDialog(null);
    switch (result) {
    case JFileChooser.APPROVE_OPTION:
        
      System.out.println("Approve (Open or Save) was clicked");
      String res = chooser.getSelectedFile().toString();
      PDFGenerator.getInstance().generatePDFForCourse(res+".pdf", course_details_controller.getInstance().getAllCourses(), 3, "COURSE DETAIL");
      System.out.println(res);
      break;
    case JFileChooser.CANCEL_OPTION:
      System.out.println("Cancel or the close-dialog icon was clicked");
      break;
    case JFileChooser.ERROR_OPTION:
      System.out.println("Error");
      break;
    }
  
    }//GEN-LAST:event_jButton4ActionPerformed

    private void courseNameTextKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_courseNameTextKeyPressed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_courseNameTextKeyPressed

    private void courseNameTextInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_courseNameTextInputMethodTextChanged
        // TODO add your handling code here:
        
    }//GEN-LAST:event_courseNameTextInputMethodTextChanged

    private void courseNameTextKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_courseNameTextKeyTyped
        // TODO add your handling code here:
        
    }//GEN-LAST:event_courseNameTextKeyTyped

    private void performSearch() {
        
        String courseName = this.courseNameText.getText();
        System.out.println(courseName);
    }
    private void courseNameTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_courseNameTextKeyReleased
        // TODO add your handling code here:
        
            String courseName = this.courseNameText.getText();
            loadData(courseName);
    }//GEN-LAST:event_courseNameTextKeyReleased

    private void jTable1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyReleased
        // TODO add your handling code here:
        
        System.out.println("Here");
    }//GEN-LAST:event_jTable1KeyReleased

    private void jTable1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyPressed
        // TODO add your handling code here:
       
    }//GEN-LAST:event_jTable1KeyPressed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        this.deleteButton.setEnabled(false);
        this.msg.setText("");
        courseIdToDelete = getTheIdForSelection();
        System.out.println(courseIdToDelete);
        if(!NullCheckUtility.isEmptyOrNull(courseIdToDelete)) {
            this.deleteButton.setEnabled(true);
        }
       
    }//GEN-LAST:event_jTable1MouseClicked

    private void noConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_noConfirmActionPerformed
        // TODO add your handling code here:
        this.conformDialogue.dispose();;
    }//GEN-LAST:event_noConfirmActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        // TODO add your handling code here:
        
        this.conformDialogue.setVisible(true);
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void yesConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_yesConfirmActionPerformed
        // TODO add your handling code here:
        if(!NullCheckUtility.isEmptyOrNull(courseIdToDelete)) {
            
            int ret = course_detail_persist.getInstance().deleteCourse(courseIdToDelete);
            if(ret!=0) {
                
                this.msg.setText("Successfully Deleted The Record");
                this.deleteButton.setEnabled(false);
                list = course_detail_persist.getInstance().getAllDetails();
                loadData(null);
            }
        }
        
        this.conformDialogue.dispose();
    }//GEN-LAST:event_yesConfirmActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(viewAllCourses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(viewAllCourses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(viewAllCourses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(viewAllCourses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new viewAllCourses().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDialog conformDialogue;
    private javax.swing.JTextField courseNameText;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel msg;
    private javax.swing.JButton noConfirm;
    private javax.swing.JButton yesConfirm;
    // End of variables declaration//GEN-END:variables

    List list = course_detail_persist.getInstance().getAllDetails();
    String courseIdToDelete;
}
